package com.xqboss.apps.service.user;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.domain.user.*;
import com.xqboss.apps.service.nft.NftService;
import com.xqboss.apps.vo.user.*;
import com.xqboss.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.mapper.user.UserIdentityPermBasicMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 用户身份-权限-装备升级上限(UserIdentityPermBasic)表服务实现类
 *
 * @author makejava
 * @since 2023-12-07 13:49:43
 */
@Service
public class UserIdentityPermBasicService extends ServiceImpl<UserIdentityPermBasicMapper, UserIdentityPermBasic> {

    @Autowired
    private UserIdentityRankService userIdentityRankService;

    @Autowired
    private UserIdentityPermIntegralService userIdentityPermIntegralService;

    @Autowired
    private UserIdentityPermWrenchService userIdentityPermWrenchService;

    @Autowired
    private NftService nftService;


    /**
     * 身份装备升级上线设定
     *
     * 身份不同段位装备升级上限 ＝ （段位数值 － 1）×  每级跨度 ＋基础等级
     *
     * @param vo 请求对象
     */
    @Transactional(rollbackFor=Exception.class)
    public void equipmentUpgradeLimit(UserIdentityPermVo vo){
        //装备升级上限
        //1 保存当前身份装备升级上线设定
        UserIdentityPermBasic userIdentityPermBasic = new UserIdentityPermBasic();
        BeanUtils.copyBeanProp(userIdentityPermBasic,vo.getUserIdentityPermBasicVo());
        this.saveOrUpdate(userIdentityPermBasic);
        //2 更新当前身份对应所有段位的最高装备升级等级
        this.userIdentityRankService.updateCurrentIdentityRankMaxEquipUpLevel(vo.getUserIdentityPermBasicVo());
        //热豆转赠
        this.userIdentityPermIntegralService.hotBeanGift(vo.getUserIdentityPermIntegralVo());
        // 扳手转增
        this.userIdentityPermIntegralService.wrenchGift(vo.getUserIdentityPermWrenchPresenterVo());
        //邀返扳手
        this.userIdentityPermWrenchService.invitationToReturnMaterials(vo.getUserIdentityPermWrenchVo());
    }

    /**
     * 装备升级上线设定
     *
     * @param identityId 身份id
     */
    public UserIdentityPermVo equipmentUpgradeInfo(Long identityId) {
        //装备升级上限
        UserIdentityPermBasic permBasic = this.lambdaQuery().eq(UserIdentityPermBasic::getIdentityId, identityId).one();
        permBasic = permBasic==null?new UserIdentityPermBasic():permBasic;
        //热豆转赠
        UserIdentityPermIntegral permIntegral = this.userIdentityPermIntegralService.hotBeanGiftInfo(identityId);
        //扳手转增
        UserIdentityPermWrenchPresenter one =  userIdentityPermIntegralService.wrenchGiftInfo(identityId);
        //邀返扳手
        UserIdentityPermWrench permWrench = this.userIdentityPermWrenchService.invitationToReturnMaterialsInfo(identityId);
        UserIdentityPermWrenchVo permWrenchVo = BeanUtil.copyProperties(permWrench, UserIdentityPermWrenchVo.class);
        // 如果有数据，则返回nfg藏品名称
        if (permWrenchVo != null && permWrenchVo.getUseNftId() != null) {
            permWrenchVo.setNftTitle(nftService.getNft(permWrenchVo.getUseNftId()).getTitle());
        }
        List<UserIdentityRank> identityRanks = this.userIdentityRankService.list(new LambdaQueryWrapper<UserIdentityRank>().eq(UserIdentityRank::getIdentityId, identityId));
        //防护权限配置vo信息
        return UserIdentityPermVo.builder()
                .userIdentityPermBasicVo(BeanUtil.copyProperties(permBasic, UserIdentityPermBasicVo.class))
                .userIdentityPermIntegralVo(BeanUtil.copyProperties(permIntegral, UserIdentityPermIntegralVo.class))
                .userIdentityPermWrenchPresenterVo(BeanUtil.copyProperties(one, UserIdentityPermWrenchPresenterVo.class))
                .userIdentityPermWrenchVo(permWrenchVo)
                .userIdentityRankVo(BeanUtil.copyToList(identityRanks, UserIdentityRankVo.class))
                .build();
    }
}
